package com.nce.util;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.nce.entity.account.User;
import com.nce.service.account.AccountManager;

public class MyAuthenticationSuccessHandler implements
		AuthenticationSuccessHandler {
	
	@Autowired
	private AccountManager accountManager;

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication auth)
			throws IOException, ServletException {	
		User user = LoginUserUtil.getUser();
		System.out.println(user);
		//user.setLoginTime(new Date());
		accountManager.saveUser(user);
		String f = request.getParameter("f");
		if (StringUtils.isNotEmpty(f)) {
			if(f.equals("android")){
				response.setCharacterEncoding("UTF-8");
				response.getWriter().write("登录成功"+LoginUserUtil.getUser());
			}
			
		}else{
			String ru = (String)request.getSession().getAttribute("ru");
			request.getSession().removeAttribute("ru");
			if(StringUtils.isNotEmpty(ru)){
				String isUseForward = (String)request.getSession().getAttribute("isUseForward");
				if(StringUtils.isNotEmpty(isUseForward) && isUseForward.equals("yes") ){
					request.getRequestDispatcher(ru).forward(request, response);
				}else{
					response.sendRedirect(ru);
				}
			}else{
				request.getRequestDispatcher("/jiankong/s/search!getNews.action").forward(request, response);
			}
			
		}

	}

}
